package com.struts.action.front;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.spring.service.front.IndexService;
import com.struts.form.FriendLinkForm;

public class IndexAction extends DispatchAction
{
	
	private IndexService indexService=null;
	
	/**
	 * index display
	 */
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		//product pic
		//recommend=1    recommend to index
		List list=indexService.findCorrelation("from Product where product_audit=1 and product_recommend=1 and is_del=0 and product_pic<>'' order by id desc limit 0,13");
		request.setAttribute("product", list);
		
		//atricle solve project
		list=indexService.findCorrelation("from Articles where is_del=0 and audit=1 and recommend=1 and leibie=1 order by id desc limit 0,10");
		request.setAttribute("project", list);
		
		//atricle services support
		list=indexService.findCorrelation("from Articles where is_del=0 and audit=1 and recommend=1 and leibie=2 order by id desc limit 0,10");
		request.setAttribute("services", list);
		
		//atricle application
		list=indexService.findCorrelation("from Articles where is_del=0 and audit=1 and recommend=1 and leibie=3 order by id desc limit 0,10");
		request.setAttribute("application", list);
		
		//friend link
		list=indexService.findCorrelation("from FriendLink where audit=1");
		request.setAttribute("friendlink", list);
		
		common(request,response);
		
		return mapping.findForward("index");
	}
	
	
	
	/**
	 * final display article
	 * @return
	 */
	public ActionForward articleDisplay(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		if(request.getParameter("articleID")!=null)
		{
			//article display
			Map map=indexService.findArticle(Long.parseLong(request.getParameter("articleID")));
			request.setAttribute("article",map );
			//atricle categry list tree
			List list=indexService.findCorrelation("from ArticlesLeibie where fatherId=6 or id=6");
			request.setAttribute("article_categry_tree", list);
			common(request,response);
			String linkurl=(String)map.get("linkurl");
			if(linkurl.trim().equals(""))
			{
				return mapping.findForward("article");
			}
			else
			{
				ActionForward forward= new ActionForward(linkurl);
				forward.setRedirect(true);
				return forward;
			}
		}
		else
		{
			return mapping.findForward("fail");
		}
	}
	
	public ActionForward articleList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		if(request.getParameter("articlesleibieID")!=null)
		{
			int page=0;
			int articlesleibieID = 0;
			int jump=0;//jump page
			if(request.getParameter("page")!=null)
			{
				page=Integer.parseInt(request.getParameter("page"));
			}
			if(request.getParameter("jump")!=null && !(request.getParameter("jump").equals("undefined")))
			{
				//System.out.println(request.getParameter("jump"));
				jump=Integer.parseInt(request.getParameter("jump"));
			}
			articlesleibieID=Integer.parseInt(request.getParameter("articlesleibieID"));
			Map hashmap=indexService.findArticleList(page,articlesleibieID,jump);
			request.setAttribute("page_content", hashmap.get("page_content"));
			request.setAttribute("article_list", hashmap.get("news_list"));
			
			//atricle categry list tree
			List list=indexService.findCorrelation("from ArticlesLeibie  where fatherId=6 or id=6");
			request.setAttribute("article_categry_tree", list);
			
			//atricle categry
			list=indexService.findCorrelation("from ArticlesLeibie where id="+articlesleibieID);
			System.out.println(list.size());
			request.setAttribute("article1_categry", list);
			
			
			common(request,response);
			return mapping.findForward("articlelist");
		}
		else
		{
			return mapping.findForward("fail");
		}
	}
	
	
	public ActionForward productList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		if(request.getParameter("productleibieID")!=null)
		{
			int page=0;
			int productleibieID = 0;
			int jump=0;//jump page
			if(request.getParameter("page")!=null)
			{
				page=Integer.parseInt(request.getParameter("page"));
			}
			if(request.getParameter("jump")!=null && !(request.getParameter("jump").equals("undefined")))
			{
				jump=Integer.parseInt(request.getParameter("jump"));
			}
			productleibieID=Integer.parseInt(request.getParameter("productleibieID"));
			Map hashmap=indexService.findProductList(page,productleibieID,jump);
			request.setAttribute("page_content", hashmap.get("page_content"));
			request.setAttribute("product_list", hashmap.get("product_list"));
			
			//atricle categry list tree
			List list=indexService.findCorrelation("from ProductLeibie");
			request.setAttribute("product_categry_tree", list);
			common(request,response);
			return mapping.findForward("productlist");
		}
		else
		{
			return mapping.findForward("fail");
		}
	}
	
	
	/**
	 * lucene search
	 * @return
	 */
	public ActionForward searchKeyword(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		if(request.getParameter("keyword")!=null && !(request.getParameter("keyword").equals("")))
		{
			int page=0;
			if(request.getParameter("page")!=null)
			{
				page=Integer.parseInt(request.getParameter("page"));
			}
			Map map=indexService.findSearchResult(request.getParameter("keyword"),page);
			//navigation
			request.setAttribute("search_result", map.get("result"));
			request.setAttribute("page_content", map.get("page_content"));
		}
		common(request,response);
		return mapping.findForward("search_success");
		
	}
	
	
	
	
	/**
	 * final display product
	 * @return
	 */
	public ActionForward productDisplay(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		if(request.getParameter("productID")!=null)
		{
			//product display
			Map map=indexService.findProduct(Long.parseLong(request.getParameter("productID")));
			request.setAttribute("product",map );
			//product categry list tree
			List list=indexService.findCorrelation("from ProductLeibie");
			request.setAttribute("product_categry_tree", list);
			common(request,response);
			return mapping.findForward("product");
		}
		else
		{
			return mapping.findForward("fail");
		}
	}
	
	
	/**
	 * 静态页跳转，为了获得导航栏的相关动态列表
	 * @return
	 */
	public ActionForward redirectUrl(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		List list=indexService.findCorrelation("from ProductLeibie");
		request.setAttribute("product_categry_tree", list);
		common(request,response);
		String param="index";
		if(request.getParameter("parameter")!=null)
		{
			param=request.getParameter("parameter");
		}
		//System.out.println(param);
		return mapping.findForward(param);
	}
	
	/**
	 * public method
	 * @param request
	 * @param response
	 */
	private void common(HttpServletRequest request, HttpServletResponse response)
	{
		//navigation
		List list=indexService.findCorrelation("from Product where product_audit=1 and product_recommend=3 and is_del=0 order by id desc limit 0,6");
		request.setAttribute("navigation", list);
		//atricle web business
		list=indexService.findCorrelation("from Articles where is_del=0 and audit=1 and leibie=4 order by id desc limit 0,10");
		request.setAttribute("webbusiness", list);
		//navigation about us
		list=indexService.findCorrelation("from Articles where audit=1 and recommend=3 and is_del=0 and leibie=5");
		request.setAttribute("aboutus", list);
		list=indexService.findCorrelation("from ArticlesLeibie where fatherId=6");
		request.setAttribute("news_center", list);
	}
	

	public IndexService getIndexService() {
		return indexService;
	}

	public void setIndexService(IndexService indexService) {
		this.indexService = indexService;
	}
}
